
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module timeoutTry
* @author: Hjava
* @description:
* @since: 2018-07-16 12:59:28
*/
/**
 * @author: wanghe02@meituan.com
 * @since: 15-4-22 上午9:45
 * @returns 返回定时器类，用于创建和维护定时器
/**
 * when timeout,try it
 * @param strategy
 * @constructor
 */
var TimeoutTry = function (strategy, host) {
    this.strategy = strategy || [30];
    this.length = this.strategy.length;
    this.cursor = 0;
    this.host = host || this;
    this.timeoutHandler = null;
    this.timeoutFun = null;
};
TimeoutTry.prototype = {
    clearTimeout: function () {
        if (this.timeoutHandler) {
            clearTimeout(this.timeoutHandler);
            this.timeoutHandler = null;
        }
    },
    timeout: function (fun) {
        this.timeoutFun = fun;
    },
    run: function (fun, delay) {
        if (delay && delay >= 0) {
            this.clearTimeout();
        }
        if (!this.timeoutHandler) {
            this.timeoutHandler = setTimeout(function () {
                this.clearTimeout();
                if (this.isEnd()) {
                    this.timeoutFun && this.timeoutFun.call(this.host);
                    return;
                }
                fun.call(this.host);
            }.bind(this), this._value(delay));
        }
    },
    /**
     * 重置
     */
    reset: function () {
        this.cursor = 0;
        this.clearTimeout();
    },
    /**
     *
     * @opional @param value
     * @returns {number}
     */
    _value: function (value) {
        if (value && value >= 0) {
            this.cursor = 0;
            while (this.cursor < this.length && value >= this.strategy[this.cursor] * 1000) {
                this.cursor++;
            }
            return value;
        }
        return this.strategy[this.cursor >= this.length ? this.length - 1 : this.cursor++] * 1000;
    },
    /**
     * 是否是最后一个值
     * @return {boolean}
     */
    isEnd: function () {
        return this.cursor >= (this.length);
    }
};
exports.default = TimeoutTry;


//# sourceURL=webpack://MTDX/./src/core/utils/timeoutTry.ts?